class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int ret = 0, zero = 0;
        for (int left = 0, right = 0; right < nums.size(); right++)
        {
            if (nums[right] == 0) zero++;
            while (zero > k)
                if (nums[left++] == 0) zero--;
            ret = max(ret, right - left + 1);
        }
        return ret;

    }
};
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size(), sum = 0;
        for (auto e : nums) sum += e;
        int target = sum - x;
        if (target < 0) return -1;
        int ret = -1, tem = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            tem += nums[right];
            while (tem > target)
                tem -= nums[left++];
            if (tem == target)
                ret = max(ret, right - left + 1);
        }
        if (ret == -1) return ret;
        else return n - ret;
    }
};
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int, int> hash;
        int n = fruits.size();
        int ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            hash[fruits[right]]++;
            while (hash.size() > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                    hash.erase(fruits[left]);
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};